package br.ufal.ic.dao.mock;

import br.ufal.ic.core.Tournament;
import br.ufal.ic.dao.TournamentDAO;
import java.util.*;

/**
 *
 * @author Marcos Paulo
 */
public class MockTournamentDAO implements TournamentDAO {

    private Map<String, Tournament> tournaments;

    public MockTournamentDAO() {
        tournaments = new HashMap<String, Tournament>();
    }

    public MockTournamentDAO(Set<Tournament> tournaments) {
        this();
        initTournaments(tournaments);
    }

    private void initTournaments(Set<Tournament> tournaments) {
        for (Tournament tournament : tournaments) {
            save(tournament);
        }
    }

    public void save(Tournament tournament) {

        if (tournament == null) {
            throw new IllegalArgumentException("infome o torneio a ser salvo");
        }

        if (tournaments.containsValue(tournament)) {
            throw new RuntimeException("torneio ja cadastrado.");
        }

        tournaments.put(tournament.getId(), tournament);
    }

    public void delete(Tournament tournament) {

        if (tournaments.remove(tournament.getId()) == null) {
            throw new RuntimeException("torneio não salvo : " + tournament);
        }
    }

    public void update(Tournament tournament) {

        delete(tournament);
        
        save(tournament);
    }

    public List<Tournament> findAll() {
        return new ArrayList<Tournament>(tournaments.values());
    }

    public Tournament findById(String id) {
        return tournaments.get(id);
    }
}
